/*
 * Venda.java
 *
 * Created on 8 de Janeiro de 2008, 14:55
 */

package br.cefetrn.datinf.pdv.visao;


import br.cefetrn.datinf.estoque.dominio.Cliente;
import br.cefetrn.datinf.estoque.dominio.EstadoItemVenda;
import br.cefetrn.datinf.estoque.dominio.Funcionario;
import br.cefetrn.datinf.estoque.dominio.ItemVenda;
import br.cefetrn.datinf.estoque.dominio.PDV;
import br.cefetrn.datinf.estoque.dominio.Venda;
import br.cefetrn.datinf.pdv.ISistema;
import br.cefetrn.datinf.pdv.Sistema;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.Timer;

/**
 *
 * @author  gleison
 */
public class VendaGui extends javax.swing.JDialog {
    
    /** Creates new form Venda */
    private ISistema sistema = Sistema.getInstance();
    private ItemVenda itemVenda = null;
    private Venda venda = null;
    private Double valor = 0.0;
    private int nItem = 0;
    private JFrame parent;
    
    
    public VendaGui(JFrame owner) {
        super(owner);
        this.parent = owner;
        initComponents();
        this.temporizador();  
        this.venda = new Venda();
         Cliente cliente = new Cliente();
         cliente.setId(1);
         Funcionario f = new Funcionario();
         f.setId(1);
         PDV pdv = new PDV();
         pdv.setID(1);
         venda.setCliente(cliente);
         venda.setFuncionario(f);
         venda.setPdv(pdv);
         
         this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e){
                setVisibleDialog(false);
                dispose();
            }            
        });
        
    }
    
    private void addItemCumpom() {
        System.out.print("Codigo: "+itemVenda.getItemProduto().getProduto().getCodigo());
        jTextAreaProdutos.append("\n"+(++this.nItem)+"  "+itemVenda.getItemProduto().getProduto().getCodigo()+"  "+
                itemVenda.getItemProduto().getProduto().getNome()+"  "
                +itemVenda.getQtde()+" X "+itemVenda.getItemProduto().getPreco() +"  "+(itemVenda.getQtde()*itemVenda.getItemProduto().getPreco())
                );
        itemVenda = new ItemVenda();
        this.limparCampos();
    }
    
    public void setVisibleDialog( boolean b ){
	parent.setEnabled(!b);
        setVisible(b);
    }

    private void encerrarVenda() {
        PagamentoTela2 pagamentoTela2 = new PagamentoTela2(venda);
        pagamentoTela2.setVisible(true);
    }

    private void finalizarVenda() {
        sistema.finalizarVenda(venda);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelPDV = new javax.swing.JLabel();
        jTextFieldPDV = new javax.swing.JTextField();
        jLabelOperador = new javax.swing.JLabel();
        jTextFieldOperador = new javax.swing.JTextField();
        jTextFieldHora = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaProdutos = new javax.swing.JTextArea();
        jLabelProdutos = new javax.swing.JLabel();
        jLabelProduto = new javax.swing.JLabel();
        jTextFieldProduto = new javax.swing.JTextField();
        jTextFieldQtd = new javax.swing.JTextField();
        jLabelQtd = new javax.swing.JLabel();
        jTextFieldSubTotal = new javax.swing.JTextField();
        jLabelSubTotal = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelPrecoUnit = new javax.swing.JLabel();
        jTextFieldPrecoUnit = new javax.swing.JTextField();
        jLabelVezes = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabelF2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ralizar Venda");

        jLabelPDV.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabelPDV.setText("PDV:");

        jTextFieldPDV.setEditable(false);
        jTextFieldPDV.setFont(new java.awt.Font("Tahoma", 0, 18));
        jTextFieldPDV.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextFieldPDV.setText("01");
        jTextFieldPDV.setBorder(null);

        jLabelOperador.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabelOperador.setText("Operador:");

        jTextFieldOperador.setEditable(false);
        jTextFieldOperador.setFont(new java.awt.Font("Tahoma", 0, 18));
        jTextFieldOperador.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextFieldOperador.setText("Gleison");
        jTextFieldOperador.setBorder(null);

        jTextFieldHora.setEditable(false);
        jTextFieldHora.setFont(new java.awt.Font("Tahoma", 0, 18));
        jTextFieldHora.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        jTextFieldHora.setText("0");
        jTextFieldHora.setBorder(null);

        jTextAreaProdutos.setColumns(20);
        jTextAreaProdutos.setEditable(false);
        jTextAreaProdutos.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTextAreaProdutos.setRows(5);
        jScrollPane1.setViewportView(jTextAreaProdutos);

        jLabelProdutos.setFont(new java.awt.Font("Tahoma", 0, 28));
        jLabelProdutos.setText("Produtos");

        jLabelProduto.setFont(new java.awt.Font("Tahoma", 0, 40));
        jLabelProduto.setText("Produto");

        jTextFieldProduto.setFont(new java.awt.Font("Tahoma", 0, 40));
        jTextFieldProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarProduto(evt);
            }
        });
        jTextFieldProduto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                atalhos(evt);
            }
        });

        jTextFieldQtd.setEditable(false);
        jTextFieldQtd.setFont(new java.awt.Font("Tahoma", 0, 40));
        jTextFieldQtd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarItem(evt);
            }
        });
        jTextFieldQtd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                atalhos(evt);
            }
        });

        jLabelQtd.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelQtd.setText("Quantidade");

        jTextFieldSubTotal.setEditable(false);
        jTextFieldSubTotal.setFont(new java.awt.Font("Tahoma", 0, 48));

        jLabelSubTotal.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabelSubTotal.setText("Subtotal");

        jSeparator1.setFont(new java.awt.Font("Tahoma", 0, 14));

        jLabelPrecoUnit.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabelPrecoUnit.setText("Preço Unitário");

        jTextFieldPrecoUnit.setEditable(false);
        jTextFieldPrecoUnit.setFont(new java.awt.Font("Tahoma", 0, 40));

        jLabelVezes.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabelVezes.setText("X");

        jLabelF2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelF2.setText("F12(Encerar)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelPDV)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldPDV, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(332, 332, 332)
                        .addComponent(jLabelOperador)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldOperador, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(274, 274, 274)
                        .addComponent(jTextFieldHora, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabelSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldSubTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                .addGap(15, 15, 15))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabelQtd)
                                            .addComponent(jTextFieldQtd, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabelVezes)
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabelPrecoUnit)
                                                .addGap(18, 18, 18))
                                            .addComponent(jTextFieldPrecoUnit)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(16, 16, 16))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelProdutos, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 432, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)))
                .addGap(13, 13, 13))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1013, Short.MAX_VALUE)
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 1013, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelF2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(916, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelPDV, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldPDV, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabelOperador, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldOperador, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextFieldHora, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelProdutos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelProduto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelQtd, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelPrecoUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldQtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldPrecoUnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelVezes, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(81, 81, 81)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelF2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void atalhos(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_atalhos
        if(evt.getKeyCode() == KeyEvent.VK_F12){
            System.out.print("F12");
            encerrarVenda();
            
        }
        else if(evt.getKeyCode() == KeyEvent.VK_F3){
            System.out.print("F3");
        }
    }//GEN-LAST:event_atalhos

    private void buscarProduto(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarProduto
        
        itemVenda = sistema.criarItemVenda(Long.parseLong(jTextFieldProduto.getText()));       
        jTextFieldProduto.setText(itemVenda.getItemProduto().getProduto().getNome());
        jTextFieldProduto.setEditable(false);
        
        jTextFieldPrecoUnit.setText(itemVenda.getItemProduto().getPreco() + "");       
        jTextFieldQtd.setEditable(true);
        jTextFieldQtd.requestFocusInWindow();
    }//GEN-LAST:event_buscarProduto

    private void adicionarItem(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarItem
        itemVenda.setQtde(Integer.parseInt(jTextFieldQtd.getText()));
        venda.adicionarItem(itemVenda);
        itemVenda.setVenda(venda);
        itemVenda.setEstado(EstadoItemVenda.ENTREGUE);
        venda.setValor(venda.getValor()+itemVenda.getItemProduto().getPreco()*itemVenda.getQtde());
        addItemCumpom();
        System.out.print("valor: "+venda.getValor()+"");
	jTextFieldSubTotal.setText(venda.getValor() + "");
        System.out.print("Enter esse");
    }//GEN-LAST:event_adicionarItem
    
     private void limparCampos() {
        jTextFieldProduto.setText(null);
        jTextFieldPrecoUnit.setText(null);
        jTextFieldQtd.setText(null);
        jTextFieldQtd.setEditable(false);
        jTextFieldProduto.setEditable(true);
        jTextFieldProduto.requestFocusInWindow();
    }
    /**
     * @param args the command line arguments
     */
    
      private void temporizador(){
        ActionListener action = new ActionListener() {
            String formato = "HH:mm:ss";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formato);
            
            
            public void actionPerformed(ActionEvent e) {
                String hora = simpleDateFormat.format(Calendar.getInstance().getTime());
                VendaGui.this.jTextFieldHora.setText(hora);
            }
        };
        
        Timer timer = new Timer(1000, action);
        timer.start();
    }
      
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelF2;
    private javax.swing.JLabel jLabelOperador;
    private javax.swing.JLabel jLabelPDV;
    private javax.swing.JLabel jLabelPrecoUnit;
    private javax.swing.JLabel jLabelProduto;
    private javax.swing.JLabel jLabelProdutos;
    private javax.swing.JLabel jLabelQtd;
    private javax.swing.JLabel jLabelSubTotal;
    private javax.swing.JLabel jLabelVezes;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextAreaProdutos;
    private javax.swing.JTextField jTextFieldHora;
    private javax.swing.JTextField jTextFieldOperador;
    private javax.swing.JTextField jTextFieldPDV;
    private javax.swing.JTextField jTextFieldPrecoUnit;
    private javax.swing.JTextField jTextFieldProduto;
    private javax.swing.JTextField jTextFieldQtd;
    private javax.swing.JTextField jTextFieldSubTotal;
    // End of variables declaration//GEN-END:variables
    
}
